import os
os.environ["HF_DATASETS_OFFLINE"] = "1"
os.environ["TRANSFORMERS_OFFLINE"] = "1"
os.environ["UNSTRUCTURED_HOME"] = r"D:\ideaSpace\MyPython\models\cache"  # 自定义缓存目录
os.environ["UNSTRUCTURED_DOWNLOAD_DIR"] = r"D:\ideaSpace\MyPython\models\cache"
os.environ["HF_HUB_OFFLINE"] = "1"

from unstructured.partition.image import partition_image
from langchain_core.documents import Document
import pytesseract

# 设置Tesseract路径（如果自动检测失败）
pytesseract.pytesseract.tesseract_cmd = r'D:\programFile\Tesseract\tesseract.exe'

image_path = r"D:\ideaSpace\rag-in-action-master\90-文档-Data\黑悟空\黑悟空英文.jpg"

# 纯OCR提取
elements = partition_image(
    filename=image_path,
    strategy="ocr_only",  # 关键参数
    languages=["chi_sim+eng"],  # 中英文混合识别
    ocr_mode="entire_page",  # 整页识别
    tesseract_config="--psm 6"  # 页面分段模式
)

# 结果处理
documents = [
    Document(
        page_content=el.text,
        metadata={"source": image_path}
    )
    for el in elements if hasattr(el, "text")
]

print("提取结果：")
for doc in documents:
    print(doc.page_content)